using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerAttack : MonoBehaviour
{
    private ActorSFX actorSFX;

    [Header("Gas Settings")]
    [SerializeField] private int damagePerTick = 10;
    [SerializeField] private float damageInterval = 1f;

    [Header("Visual Settings")]
    [SerializeField] private ParticleSystem gasParticles;

    [Header("Music Settings")]
    [SerializeField] private AudioClip playerLayerSound;

    private HashSet<ZombiePatrollerHealth> zombiesInGas = new HashSet<ZombiePatrollerHealth>();
    private Coroutine damageCoroutine;

    private void Awake()
    {
        actorSFX = GetComponent<ActorSFX>();
    }

    private void Start()
    {
        if (gasParticles != null)
        {
            gasParticles.Play();
        }

        if (actorSFX != null && playerLayerSound != null)
        {
            actorSFX.PlayLoopingSFX(playerLayerSound);
        }

        damageCoroutine = StartCoroutine(DamageEnemiesOverTime());
    }

    private void OnTriggerEnter(Collider other)
    {
        ZombiePatrollerHealth zombieHealth = other.GetComponent<ZombiePatrollerHealth>();
        if (zombieHealth != null)
        {
            zombiesInGas.Add(zombieHealth);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        ZombiePatrollerHealth zombieHealth = other.GetComponent<ZombiePatrollerHealth>();
        if (zombieHealth != null)
        {
            zombiesInGas.Remove(zombieHealth);
        }
    }

    private IEnumerator DamageEnemiesOverTime()
    {
        while (true)
        {
            DamageEnemiesInArea();
            yield return new WaitForSeconds(damageInterval);
        }
    }

    private void DamageEnemiesInArea()
    {
        foreach (ZombiePatrollerHealth zombie in zombiesInGas)
        {
            if (zombie != null)
            {
                zombie.TakeDamage(damagePerTick);
            }
        }
    }
}